package com.vdong.trade.order.common.foundation;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Id;
import java.lang.reflect.Field;
import java.util.Date;

/**
 * @description: DO超类
 * @author: Mr.WangGang
 * @create: 2018-10-23 上午 10:35
 **/
interface ID{
    Long getId();

    void setId(Long id);
}

@Data
public abstract class FoundationMO implements ID{

    private Long id;

    public void timeSet() {
        Date now = new Date();
        Class clazz = this.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            Id id = f.getAnnotation(Id.class);
            String name = f.getName();
            if(id!=null){
                if(this.getId()!=null){
                    this.setUpdateTime(now);
                }else {
                    this.setCreateTime(now);
                    this.setUpdateTime(now);
                }
                break;
            }
        }
    }


    @Column(name="create_time")
    private Date createTime;

    @Column(name="update_time")
    private Date updateTime;

}
